<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
            <div class="page">
 2             <a href="${page.path}&pageNumber=1">首页</a>
 3             <a href="${page.path}&pageNumber=${page.pageNumber-1}">上一页</a>
 4             <!-- 
 5                 始终保证当前页在中间，一共显示5页 
 6                 1.总页码<=5时，显示所有页数
 7                 2.总页码>5时：
 8                     当前页码<=3时：显示1~5页
 9                     当前页码>3时：显示当前页在中间的5页,当当前页为倒数前3页时，显示最后5页
10             -->
11             <c:choose>
12                 <c:when test="${page.totalPage<=5}">
13                     <c:set var="begin" value="1"></c:set>
14                     <c:set var="end" value="${page.totalPage}"></c:set>
15                 </c:when>
16                 <c:when test="${page.pageNumber<=3}">
17                     <c:set var="begin" value="1"></c:set>
18                     <c:set var="end" value="5"></c:set>
19                 </c:when>
20                 <c:when test="${page.pageNumber>3}">
21                     <c:set var="begin" value="${page.pageNumber-2}"></c:set>
22                     <c:set var="end" value="${page.pageNumber+2}"></c:set>
23                     <c:if test="${page.pageNumber+2>=page.totalPage}">
24                         <c:set var="begin" value="${page.totalPage-4}"></c:set>
25                         <c:set var="end" value="${page.totalPage}"></c:set>
26                     </c:if>
27                 </c:when>
28             </c:choose>
29             <!-- 通过循环显示由begin到end的5个页面，当前页用【】标识 -->
30             <c:forEach begin="${begin}" end="${end}" var="index">
31                 <c:if test="${page.pageNumber==index}">
32                     <a href="${page.path}&pageNumber=${index}">【${index}】</a>
33                 </c:if>
34                 <c:if test="${page.pageNumber!=index}">
35                     <a href="${page.path}&pageNumber=${index}">${index}</a>
36                 </c:if>
37             </c:forEach>
38             
39             <a href="${page.path}&pageNumber=${page.pageNumber+1}">下一页</a>
40             第${page.pageNumber}页，共${page.totalPage}页
41             转到第<input id="setPage" type="text" value="${page.pageNumber}"/>页，<a id="goto" href="">跳转</a>
42             <a href="${page.path}&pageNumber=${page.totalPage}">末页</a>
43 <script type="text/javascript">
44     window.onload=function(){
45         var agoto=document.getElementById("goto");
46         agoto.onclick=function(){
47             var setPage=document.getElementById("setPage").value;
48             window.location="${page.path}&pageNumber="+setPage;
49             return false;
50         };
51     };
52 </script>
53 <!--或 <script type="text/javascript">
54     $(function(){
55         $("#goto").click(function(){
56             var $setPage=$("#setPage").val();
57             window.location="/MyBookStore/book?method=getPage&pageNumber="+$setPage;
58             return false;
59         });
60     });
61 </script> -->
62         </div>
  </body>
</html>
